package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.activity.Classify_ad_Activity;
import com.qianfeng.wuyang.himalaya.activity.RecommendedFocusActivity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.FocusImgInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by WangQing on 2016/12/6.
 */

public class RecommendFocusAdapter extends PagerAdapter implements View.OnClickListener{
    private Context context;
    private List<View> list;
    private List<FocusImgInfoEntity> infoList = new ArrayList<>();

    public RecommendFocusAdapter(List<View> list,Context context) {
        this.list =new ArrayList<>();
        this.context = context;
    }

    public void addData(List<View> viewList,List<FocusImgInfoEntity> infoList){
//        if (list==null) {
            list.addAll(viewList);
        this.infoList.addAll(infoList);

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        list.get(position).setOnClickListener(this);
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }

    @Override
    public void onClick(View view) {
        int position=((ViewPager)view.getParent()).getCurrentItem();

        //还有两个虚假页面 所以 position - 1

        if (infoList.get(position-1).getUrl()!=null) {
                Intent intent = new Intent(context, Classify_ad_Activity.class);
                intent.putExtra("url",infoList.get(position-1).getUrl());
                context.startActivity(intent);
            }
            else {
                if (infoList.get(position-1).getTrackId()!=null) {
                    Intent intent1=new Intent(context, RecommendedFocusActivity.class);
                    intent1.putExtra("id",infoList.get(position).getTrackId());
                    context.startActivity(intent1);
                }

                Toast.makeText(context,"无跳转界面",Toast.LENGTH_LONG).show();
            }

    }
}
